package com.helloae.chap07;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.FieldChecker;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.GeometryDef;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureClassProxy;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.IProjectedCoordinateSystem;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRProjCSType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 为地理坐标数据投影<br>
 * 示例章节：7.6.1
 * @author light
 *
 */
public class ProjectShapefile {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//设置输入文件参数
		WorkspaceName pInWSName = new WorkspaceName();
		pInWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");

		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		pInWSName.setPathName(ecliseWorkspace + "HelloAE" + File.separator + "DataSet" +File.separator + "chap07");

		FeatureClassName pInFCName = new FeatureClassName();
		pInFCName.setName("idll27");
		pInFCName.setWorkspaceNameByRef(pInWSName);
		IFeatureClass pInFC = new IFeatureClassProxy(pInFCName.open());
		
		//验证输入数据的字段是否合法，合法则输出成输出字段
		//这里中间的参数不宜为null
		IFields pInFCFields = pInFC.getFields();
		Fields[] pOutFCFields = new Fields[1];
		FieldChecker pFieldChecker = new FieldChecker();
		pFieldChecker.validate(pInFCFields, null, pOutFCFields);
		
		//获得geo字段
		IField pGeoField = null;
		for(int i = 0; i < pOutFCFields[0].getFieldCount(); i++){
			if(pOutFCFields[0].getField(i).getType() == esriFieldType.esriFieldTypeGeometry){
				pGeoField = pOutFCFields[0].getField(i);
			}
		}
		
		//生成NAD1927UTM_11N坐标
		SpatialReferenceEnvironment pSpatRefFact = new SpatialReferenceEnvironment();
		IProjectedCoordinateSystem pPCS = pSpatRefFact.createProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1927UTM_11N);
		
		//生成用于转换的关于坐标的参数
		GeometryDef pOutFCGeoDefEdit = (GeometryDef) pGeoField.getGeometryDef();
		pOutFCGeoDefEdit.setGridCount(1);
		pOutFCGeoDefEdit.setGridSize(0, 200);
		pOutFCGeoDefEdit.setSpatialReferenceByRef(pPCS);
		
		//配置输出参数
		WorkspaceName pOutWSName = new WorkspaceName();
		pOutWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");

		pOutWSName.setPathName("c:\\temp");

		FeatureClassName pOutFCName = new FeatureClassName();
		pOutFCName.setName("idutm27");
		pOutFCName.setWorkspaceNameByRef(pOutWSName);
		
		//转换
		FeatureDataConverter pFDConverter = new FeatureDataConverter();
		pFDConverter.convertFeatureClass(pInFCName, null, null, pOutFCName, pOutFCGeoDefEdit, pOutFCFields[0], "", 1000, 0);
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
